
#import "UIView+Create.h"

@implementation UIView (Create)

+ (UIView *)viewWithColor:(UIColor *)color
                    frame:(CGRect)frame
                superView:(UIView *)superView
          constraintMaker:(void (^)(MASConstraintMaker *))maker {
    
    UIView *view = [[self alloc] initWithFrame:frame];
    view.backgroundColor = color;
    
    if (superView) {
        [superView addSubview:view];
    } else {
        return view;
    }
    
    if (maker) {
        [view mas_makeConstraints:maker];
    }
    
    return view;
}

@end


@implementation UIButton (Create)

+ (UIButton *)buttonTitle :(NSString *)title
                     frame:(CGRect)frame
                      font:(NSInteger)font
                titleColor:(UIColor *)titleColor
                 imageName:(id)imageName
            backgroundImge:(id)backgroundImage
                    target:(id)target action:(SEL)action
                 superView:(UIView *)superView
           constraintMaker:(void (^)(MASConstraintMaker *))maker
{
    
    UIButton *button = [self buttonWithType:UIButtonTypeCustom];
    button.frame = frame;
    UIFont *labelFont = [UIFont systemFontOfSize:font];
    button.titleLabel.font = labelFont;
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    if (title) {
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:titleColor forState:UIControlStateNormal];
    }
    
    if (imageName && [imageName isKindOfClass:[NSString class]]) {
        [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    } else if (imageName && [imageName isKindOfClass:[UIImage class]]) {
        [button setImage:imageName forState:UIControlStateNormal];
    }
    
    if (backgroundImage && [backgroundImage isKindOfClass:[NSString class]]) {
        [button setBackgroundImage:[UIImage imageNamed:backgroundImage] forState:UIControlStateNormal];
    } else if (backgroundImage && [backgroundImage isKindOfClass:[UIImage class]]) {
        [button setBackgroundImage:backgroundImage forState:UIControlStateNormal];
    }
    
    if (target && action) {
        [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    
    if (superView) {
        [superView addSubview:button];
    } else {
        return button;
    }
    
    //布局
    if (maker) {
        [button mas_makeConstraints:maker];
    }
    return button;
}

@end


@implementation UILabel (Create)

+ (UILabel *)labelWithTitle:(NSString *)title
                 titleColor:(UIColor *)titleColor
                       font:(CGFloat)font
                      frame:(CGRect)frame
                  superView:(UIView *)superView
            constraintMaker:(void (^)(MASConstraintMaker *))maker {
    
    UILabel *label = [[self alloc] initWithFrame:frame];
    label.textColor = titleColor;
    label.text = title;
    label.font = [UIFont systemFontOfSize:font];
    if (superView) {
        [superView addSubview:label];
    } else {
        return label;
    }
    
    if (maker) {
        [label mas_makeConstraints:maker];
    }
    
    return label;
}

@end


@implementation UIImageView (Create)

+ (UIImageView *)imageViewWithImageName:(id)imageName
                                  frame:(CGRect)frame
                              superView:(UIView *)superView
                        constraintMaker:(void (^)(MASConstraintMaker *))make {
    
    UIImageView *imageView = [[self alloc]initWithFrame:frame];
    if (imageName && [imageName isKindOfClass:[NSString class]]) {
        imageView.image = [UIImage imageNamed:imageName];
    } else if (imageName && [imageName isKindOfClass:[UIImage class]]) {
        imageView.image = imageName;
    }
    
    if (superView) {
        [superView addSubview:imageView];
    } else {
        return imageView;
    }
    
    if (make) {
        [imageView mas_makeConstraints:make];
    }
    
    return imageView;
}

@end
